// import { NODE_TYPE_LABEL, NODE_TYPE } from '@/nodes/constant';
/*
组件联动关系 数据维护
*/

/*
step 1
let nodeChildrenMap = {
    1:[4],
    2:[3,4],
    3:[5],
    4:[5,6],
    5:[6],
    6:[]
};
step 2
let nodeChildrenMap = {
    1:[{node:4,children:[]}],
    2:[{node:3,children:[]},{node:4,children:[]}],
    3:[{node:5,children:[]}],
    4:[{node:5,children:[]},{node:6,children:[]}],
    5:[{node:6,children:[]}],
    6:[]
};
step 3
//正向树 可以获取组件的子级
let nodeChildrenMap = {
    1:[{node:4,children:nodeChildrenMap.2}],
    2:[{node:3,children:nodeChildrenMap.3},{node:4,children:nodeChildrenMap.4}],
    3:[{node:5,children:nodeChildrenMap.5}],
    4:[{node:5,children:nodeChildrenMap.5},{node:6,children:nodeChildrenMap.6}],
    5:[{node:6,children:nodeChildrenMap.6}],
    6:[]
};
step 4
//需要知道某个节点的上级有哪些
let nodeParentMap = {
    1:[],
    2:[],
    3:[2],
    4:[1,2],
    5:[3,4],
    6:[4,5]
};
step 4
//需要知道某个节点的所有上级有哪些
let nodeParentsMap = {
    1:[],
    2:[],
    3:[{node:2,parent:nodeParentsMap.2}],
    4:[{node:1,parent:nodeParentsMap.1},{node:2,parent:nodeParentsMap.2}],
    5:[{node:3,parent:nodeParentsMap.3},{node:4,parent:nodeParentsMap.4}],
    6:[{node:4,parent:nodeParentsMap.4},{node:5,parent:nodeParentsMap.5}]
};

*/

class ComponentLinkage {
    updateInfo = (nodes) => {
        const nodeChildrenMap = {
            // 1:[4],
            // 2:[3,4],
            // 3:[5],
            // 4:[5,6],
            // 5:[6],
            // 6:[]
        };
        nodes.forEach(node => {
            const { nid, props: { behaviorLinkage = [] } } = node;
            nodeChildrenMap[nid] = nodeChildrenMap[nid] || [];
            behaviorLinkage.forEach(item => {
                const { targets } = item;
                targets.forEach(i => {
                    nodeChildrenMap[nid].push({
                        nodeId: i.fieldId,
                        children: null
                    });
                });
            });
        });

        const nodeParentMap = {};
        for (const nodeId in nodeChildrenMap) {
            nodeChildrenMap[nodeId].forEach(item => {
                item.children = nodeChildrenMap[item.nodeId];

                nodeParentMap[item.nodeId] = nodeParentMap[item.nodeId] || [];
                nodeParentMap[item.nodeId].push({
                    nodeId,
                    parents: null
                });
            });
        }

        for (const nodeId in nodeParentMap) {
            nodeParentMap[nodeId].forEach(item => {
                item.parents = nodeParentMap[item.nodeId];
            });
        }
        this.nodeChildrenMap = nodeChildrenMap;
        this.nodeParentMap = nodeParentMap;
    }
    //根据组件id 获取可以关联的组件
    isNodeVisible({ nodes, id }) {
        const self = nodes.find(node => node.nid === id);
        const nodeParentMap = this.nodeParentMap || {};
        const parents = nodeParentMap[self.nid];
        //如果没有上级 或者上级的长度为0 直接显示
        if (!parents || parents.length === 0) {
            return true;
        }
        function isVisible(selfNodeId, data) {
            const { nodeId, parents } = data;
            const node = nodes.find(node => node.nid === nodeId);
            const condition = node.props.behaviorLinkage.find(item => {
                return item.targets.map(item => item.fieldId).includes(selfNodeId);
            });
            //如果此节点的上级没有一个显示 则此节点 也不现实
            if (parents && parents.length && !parents.some(parent => isVisible(nodeId, parent))) {
                return false;
            }
            if (node.type === 'multi_select') {
                return String(node.value || '').includes(condition.value);
            }
            return condition.value === node.value;
        }
        return (parents || []).some(parent => {
            return isVisible(self.nid, parent)
        });
    }
}
const componentLinkage = new ComponentLinkage();
export default componentLinkage;